package com.sinoglobal.shakeprize.activity.address;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract.Constants;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.shakeprize.R;
import com.sinoglobal.shakeprize.activity.base.AbstractActivity;
import com.sinoglobal.shakeprize.activity.prize.MyPrizeGetTheWay;
import com.sinoglobal.shakeprize.adapter.AddressListAdapter;
import com.sinoglobal.shakeprize.bean.PrizeAddressDetails;
import com.sinoglobal.shakeprize.bean.ResponsePrizeAddress;
import com.sinoglobal.shakeprize.utils.Contstent;
import com.sinoglobal.shakeprize.utils.OKhttpUtil;
import com.sinoglobal.shakeprize.utils.OKhttpUtil.ResponseCallback;
import com.sinoglobal.shakeprize.utils.SPUtil;

import okhttp3.Request;


public class PrizeAddress extends AbstractActivity implements OnClickListener {
	private static final int ADDRESS_INFO = 1;
	private static final int ADDRESS_FAIL = 2;

	private Bundle bundle;
	private ListView addressListView;
	private Button shakePrizeAddressNext;
	private ArrayList<PrizeAddressDetails> addressDetails = new ArrayList<PrizeAddressDetails>();
	private AddressListAdapter listAdapter;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ADDRESS_INFO:
				listAdapter.setData(addressDetails);
				break;
			case ADDRESS_FAIL:
				Toast.makeText(PrizeAddress.this, "访问网络失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shake_activity_prize_address);
		bundle = getIntent().getExtras();
		templateButtonRight.setText("新建地址");
		templateButtonRight.setOnClickListener(this);
		addressListView = (ListView) findViewById(R.id.shakePrizeAddressList);
		shakePrizeAddressNext = (Button) findViewById(R.id.shakePrizeAddressNext);
		shakePrizeAddressNext.setOnClickListener(this);
		listAdapter = new AddressListAdapter(this);
		addressListView.setAdapter(listAdapter);
		addressListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

			}
		});
		addressListView
				.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {
						return false;
					}
				});

	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private void initData() {
		// http://192.168.10.11:8013/app_v1.3.0/?por=5001&user_id=795&proIden=8a7dd37255b778b4062deac6dbd5ada9
		String url = "http://192.168.10.11:8013/app_v1.3.0/?por=5001&user_id="
				+ SPUtil.getString(mActivity, Contstent.USER_ID) + "&proIden=" + Contstent.STOREFLAG;
		OKhttpUtil.enqueue(url, new ResponseCallback() {

			private ResponsePrizeAddress address;

			@Override
			public void onFailure(Request arg0, IOException arg1) {

			}

			@Override
			public void onResponse(String json) throws IOException {
				try {
					address = JSON
							.parseObject(json, ResponsePrizeAddress.class);
				} catch (Exception e) {
					handler.sendEmptyMessage(ADDRESS_FAIL);
					return;
				}
				if (address.list.size() > 0) {
					for (int i = 0; i < address.list.size(); i++) {
						if ("1".equals(address.list.get(i).is_default)) {
							address.list.get(i).isChecked = true;
						}
					}
					addressDetails = address.list;
					handler.sendEmptyMessage(ADDRESS_INFO);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		if (v.getId()==R.id.title_but_right) {
			// 跳转到添加收货地址页面
						intent.setAction("com.sinoglobal.sinostore.activity.SelAddressActivity");
						intent.putExtra("isAdd", true);
						startActivity(intent);
		}else if (v.getId()==R.id.shakePrizeAddressNext) {
			if (addressDetails.size() > 0
					&& listAdapter.getCheckedPosition() != null) {
				intent.setClass(this, MyPrizeGetTheWay.class);// 跳转到确认收货方式页面
				bundle.putString("address",
						listAdapter.getCheckedPosition().address);
				bundle.putString("area", listAdapter.getCheckedPosition().area);
				bundle.putString("connect_tel",
						listAdapter.getCheckedPosition().connect_tel);
				bundle.putString("connect_name",
						listAdapter.getCheckedPosition().connect_name);
				intent.putExtras(bundle);
				startActivity(intent);
			} else {
				Toast.makeText(mActivity, "请添加收货地址", 0).show();
			}
		}
	}

}
